package com.xzmzhp.service.activity.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzmzhp.dao.activity.ProgramLibraryMapper;
import com.xzmzhp.pojo.activity.Program;
import com.xzmzhp.pojo.activity.ProgramLibrary;
import com.xzmzhp.service.activity.ProgramLibraryService;
import com.xzmzhp.service.activity.ProgramService;
import com.xzmzhp.utils.DateUtil;
import com.xzmzhp.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-03-31 09:53
 * @program: ams
 * @description: 节目库逻辑接口实现类
 */
@Service
public class ProgramLibraryServiceImpl implements ProgramLibraryService {

    @Autowired
    private ProgramLibraryMapper programLibraryMapper;

    @Autowired
    private ProgramService programService;

    /**
     * 查询所有节目数据
     * @return 节目库数据
     */
    @Override
    public List<ProgramLibrary> findAll() {
        QueryWrapper<ProgramLibrary> queryWrapper = new QueryWrapper<>();
        //查询指定某字段以外的数据
        queryWrapper.select(ProgramLibrary.class, info ->!"program_content".equals(info.getColumn()));
        return programLibraryMapper.selectList(queryWrapper);
    }

    /**
     * 根据参数查询数据
     * @param param 参数
     *        programName 节目名称
     *        programTypeId 节目类型Id
     *        programTypeName 节目类型名称
     *        startTime 开始时间
     *        endTime 结束时间
     * @return 节目库数据
     */
    @Override
    public List<ProgramLibrary> findProgramLibraryByParam(Map<String, Object> param) {
        //获取参数
        String programName =  param.get("programName").toString();
        Integer programTypeId = Integer.parseInt(param.get("programTypeId").toString());
        String programTypeName =  param.get("programTypeName").toString();
        Date startTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("startTime").toString());
        Date endTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("endTime").toString());
        //封装sql语句
        QueryWrapper<ProgramLibrary> queryWrapper = new QueryWrapper<>();
        //查询指定某字段以外的数据
        queryWrapper.select(ProgramLibrary.class, info ->!"program_content".equals(info.getColumn()));
        //判断参数是否为空
        if (StringUtils.isNotEmpty(programName)){
            queryWrapper.like("program_name",programName);
        }
        if (StringUtils.isNotNull(programTypeId)){
            queryWrapper.eq("program_type_id",programTypeId);
        }
        if (StringUtils.isNotEmpty(programTypeName)){
            queryWrapper.like("program_type_name",programTypeName);
        }
        if (StringUtils.isNotNull(startTime)){
            queryWrapper.ge("program_creation_time",startTime);
        }
        if (StringUtils.isNotNull(endTime)){
            queryWrapper.le("program_creation_time",endTime);
        }
        return programLibraryMapper.selectList(queryWrapper);
    }

    /**
     * 根据id查询节目内容
     * @param programId 节目id
     * @return 节目内容
     */
    @Override
    public String findContentById(Integer programId) {
        QueryWrapper<ProgramLibrary> queryWrapper = new QueryWrapper<>();
        //指定查询某字段
        queryWrapper.select("program_content");
        ProgramLibrary programLibrary = programLibraryMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(programLibrary)){
            return programLibrary.getProgramContent().toString();
        }
        return null;
    }

    /**
     * 根据节目id查询节目信息
     * @param programId 节目信息
     * @return 节目信息
     */
    @Override
    public ProgramLibrary findProgramLibraryById(Integer programId) {
        return programLibraryMapper.selectById(programId);
    }

    /**
     * 根据节目名称查询节目信息
     * @param programName 节目名称
     * @return 节目信息
     */
    @Override
    public ProgramLibrary findProgramLibraryByProgramName(String programName) {
        QueryWrapper<ProgramLibrary> queryWrapper = new QueryWrapper<>();
        //指定查询某字段
        queryWrapper.select("program_name");
        return programLibraryMapper.selectOne(queryWrapper);
    }

    /**
     * 添加节目库信息
     * @param programLibrary 节目库信息
     * @return 是否成功
     */
    @Override
    public Integer addProgramLibrary(ProgramLibrary programLibrary) {
        if (StringUtils.isNotNull(programLibrary)){
            //查询名称是否重复
            ProgramLibrary program = findProgramLibraryByProgramName(programLibrary.getProgramName());
            if (StringUtils.isNotNull(program)){
                return programLibraryMapper.insert(programLibrary);
            }
        }
        return null;
    }

    /**
     * 修改节目信息
     * @param programLibrary 节目库信息
     * @return 是否成功
     */
    @Override
    public Integer exitProgramLibrary(ProgramLibrary programLibrary) {
        return programLibraryMapper.updateById(programLibrary);
    }

    /**
     * 根据节目id删除节目信息(仅允许未使用的节目被删除)
     * @param programId 节目id
     * @return 是否成功
     */
    @Override
    public Integer delProgramLibrary(Integer programId) {
        //查询是否使用
        List<Program> program = programService.findProgramByProgramId(programId);
        if (StringUtils.isNotEmpty(program)){
            return null;
        }
        return programLibraryMapper.deleteById(programId);
    }
}
